﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PayloadTracker.APRS
{
    public class Tools
    {
        /// <summary>
        /// Convert GPS string to decimal degrees.
        /// </summary>
        /// <param name="GPS"></param>
        /// <param name="Bearing"></param>
        /// <returns></returns>
        public static double GPSToDecimalDegrees(string GPS, string Bearing)
        {
            try
            {
                if (GPS == string.Empty || Bearing == string.Empty)
                    return double.MinValue;

                //Get the fractional minutes
                string tmp = GPS.Substring(GPS.IndexOf("."));
                double FractionalMinutes = double.Parse(GPS.Substring(GPS.IndexOf(".")));

                //Get the minutes.
                tmp = GPS.Substring(GPS.IndexOf(".") - 2, 2);
                double Minutes = double.Parse(GPS.Substring(GPS.IndexOf(".") - 2, 2));

                //Get the degrees
                tmp = GPS.Substring(0, GPS.IndexOf(".") - 2);
                double Degrees = double.Parse(GPS.Substring(0, GPS.IndexOf(".") - 2));


                double DecimalDegrees = Degrees + (Minutes + FractionalMinutes) / 60;
                return (Bearing == "S" || Bearing == "W") ? -DecimalDegrees : DecimalDegrees;
            }
            catch
            {
                return double.MinValue;
            }
        }
        /// <summary>
        /// Convert NMEAUTC time to a DateTime type.
        /// </summary>
        /// <param name="NMEAUTCTime"></param>
        /// <returns></returns>
        public static DateTime NMEAUTCTimeToUTCDateTime(string NMEAUTCTime)
        {
            TimeSpan ts = new TimeSpan(int.Parse(NMEAUTCTime.Substring(0, 2)),
                                       int.Parse(NMEAUTCTime.Substring(2, 2)),
                                       int.Parse(NMEAUTCTime.Substring(4, 2)));

            var UTCTime = DateTime.UtcNow.Add(-DateTime.UtcNow.TimeOfDay).Add(ts);

            return UTCTime;
        }

        public static float MetersToFeet(double meters)
        {
            return ((float)(meters * 3.2808399));
        }
        public static float KnotsToMPH(double knots)
        {
            return ((float)(knots * 1.15077945));
        }

        public static double DegreesToRadians(double degrees)
        {
            return degrees * Math.PI / 180.0;
        }
        private static double RadiansToDegrees(double radians)
        {
            return radians * (180.0 / Math.PI);
        }

    }
}
